#include "../includes/DiskDriver.h"

DiskDriver* DiskDriver::instance_ = nullptr;

DiskDriver& DiskDriver::getInstance(const std::string &path){
    if(instance_ == nullptr){
        instance_ = new DiskDriver(path);
    }
    return *instance_;
}

// 默认的有参构造函数
DiskDriver::DiskDriver(const std::string &path){
    file_path_ = path;
    fd_ = open(path.c_str(), O_RDWR); // 打开文件, 获得文件句柄
}

ssize_t DiskDriver::d_read(void *buff, size_t size, off_t offset){
    return pread(fd_, buff, size, offset);
}

ssize_t DiskDriver::d_write(void *buff, size_t size, off_t offset){
    return pwrite(fd_, buff, size, offset);
}

DiskDriver::~DiskDriver(){
    close(fd_); // 关闭文件描述符
}